package com.zl.smartmall.library;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import android.content.Context;

import com.loopj.android.http.RequestParams;
import com.zl.smartmall.library.account.IAuth;
import com.zl.smartmall.library.listener.AddToShoppingCartListener;
import com.zl.smartmall.library.listener.AddToShoppingCartMultiListener;
import com.zl.smartmall.library.listener.DeleteGoodsListener;
import com.zl.smartmall.library.listener.RequestCode;
import com.zl.smartmall.library.listener.ShoppingCartListener;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.util.SingtonAsyncHttpClient;
import com.zl.smartmall.library.util.Tools;

/**
 * 购物车相关操作接口
 * @author 陈希然
 *
 */
public class IShoppingCart {
	
	private static final String TAG = IShoppingCart.class.getSimpleName();
	private static IShoppingCart mInstance;
	private static Lock mLock;
	private Context mContext;
	
	static{
		mLock = new ReentrantLock();
	}
	
	private IShoppingCart(Context context) {
		mContext = context;
	}
	
	public static IShoppingCart getInstance() {
		return mInstance;
	}
	
	public static void init(Context context) {
		if(mInstance == null){
    		mLock.lock();
    		try{
				if(mInstance == null){
					mInstance = new IShoppingCart(context);
				}
    		}finally{
    			mLock.unlock();
    		}
    	}
	}
	
	/**
	 * 添加单种商品到购物车
	 * @param activitiesId 活动id
	 * @param shopId 商家id
	 * @param productId 商品id
	 * @param num 添加数量
	 * @param addToShoppingCartListener 回调监听器
	 */
	public void addToShoppingCart(int activitiesId, int shopId, int productId, int num, AddToShoppingCartListener addToShoppingCartListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			addToShoppingCartListener.onNoNetWork();
			
		}else {
			RequestParams params = new RequestParams();
			params.put("act_id", activitiesId);
			params.put("shop_id", shopId);
			params.put("product_id", productId);
			params.put("num", num);
			params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_ADD_PRODUCT_TO_CART, params, addToShoppingCartListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_ADD_PRODUCT_TO_CART + "?" + params);
		}
	}
	
	/**
	 * 添加多种商品到购物车
	 * @param commodities 商品数据; 如：[{"act_id":1,"shop_id":3,"product_id":5,"num":45},{"act_id":2,"shop_id":3,"product_id":5,"num":45}]
	 * @param addToShoppingCartMultiListener 
	 */
	public void addToShoppingCartMulti(String commodities, AddToShoppingCartMultiListener addToShoppingCartMultiListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			addToShoppingCartMultiListener.onNoNetWork();
			
		}else {
			RequestParams params = new RequestParams();
			params.put("goods", commodities);
			params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_ADD_PRODUCT_TO_CART_MULTI, params, addToShoppingCartMultiListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_ADD_PRODUCT_TO_CART_MULTI + "?" + params);
		}
	}

	/**
	 * 获取购物车
	 * @param shoppingCartListener 回调监听器
	 */
	public void getShoppingCart(ShoppingCartListener shoppingCartListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			shoppingCartListener.onNoNetWork();
			
		}else {
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			RequestParams params = new RequestParams();
			params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_SHOPPING_CART, params,shoppingCartListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_SHOPPING_CART);
		}
	}

	/**
	 * 删除购物车商品
	 * @param mDeleteStr 需要删除的商品
	 * @param deleteGoodsListener 回调监听器
	 */
	public void deleteGoods(String mDeleteStr, DeleteGoodsListener deleteGoodsListener) {
		if(!Tools.hasInternetConnection(mContext)) {
			deleteGoodsListener.onNoNetWork();
			
		}else {
			RequestParams params = new RequestParams();
			params.put("goods", mDeleteStr);
			params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_SHOPPING_CART_DELETE, params, deleteGoodsListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_SHOPPING_CART_DELETE + "?" + params);
		}
	}
	
	public void upDataGoods(String goodsStr, ShoppingCartListener shoppingCartListener){
		if(!Tools.hasInternetConnection(mContext)) {
			shoppingCartListener.setRequestCode(RequestCode.UPDATA);
			shoppingCartListener.onNoNetWork();
			
		}else {
			shoppingCartListener.setRequestCode(RequestCode.UPDATA);
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			RequestParams params = new RequestParams();
			params.put("goods", goodsStr);
			params.put("cmid", IGZAddress.getInstance().getCurrentCommunityID());
			SingtonAsyncHttpClient.getInstance().post(Constants.URL_SHOPPING_CART_UPDATE, params, shoppingCartListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_SHOPPING_CART_UPDATE + "?" + params);
		}
	}
}
